package edu.hawaii.webspider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import org.junit.Test;


/**
 * A demonstration class illustrating how to test WebSpiderExample class.
 * 
 * @author Chiao-Fen Zielinski-Liu
 */
public class TestWebSpiderExample {
  String testUrl = "http://hackystat.org";
  WebSpiderExample testWebSpider = new WebSpiderExample(testUrl);
  
  /**
   * @throws Exception if problems occur testing the main method
   */
  @Test (expected = AssertionError.class)
  public void testMain() throws Exception {
    try {
      WebSpiderExample.main(new String[0]);
      fail("fail to test for main()");
    }
    catch (AssertionError e) {
      throw new AssertionError(e);
    }
  }

  /**
   * A demonstration to test the find most popular page.
   * @throws Exception if problems occur
   */
  @Test
  public void testFindMostPopularPage() throws Exception {
    String[] test = new String[3];
    test[0] = "-mostpopular";
    test[1] = testUrl;
    test[2] = "5";
    
    WebSpiderExample.main(test);
    assertEquals ("test two urls", testUrl, testWebSpider.startUrl);
    
    test[0] = "-totallinks";
    WebSpiderExample.main(test);
  }
  
  /**
   * A demonstration to test the find total page that points to one link.
   * @throws Exception if problems occur
   */
  @Test
  public void testFindTotalPage() throws Exception {
    String[] test = new String[3];
    test[0] = "-mostpopular";
    test[1] = "http://hackystat.org/hackyDevSite/home.do";
    test[2] = "5";
    
    WebSpiderExample.main(test);
    assertEquals ("test these urls", testUrl, testWebSpider.startUrl);
  }
  
  /**
   * @throws Exception if problems occur retrieving the number of links
   */
  @Test
  public void testGetNumLinks() throws Exception {
    WebSpiderExample testWebSpider = new WebSpiderExample("http://www.httpunit.org");
    assertEquals("Testing the number of links in a given url", 26, testWebSpider.getNumLinks());
  }
  
  /**
   * A demonstration to test the isNum method.
   * @throws Exception if problems occur retrieving the number of links
   */
  @Test (expected = AssertionError.class)
  public void testIsNumber() throws Exception {
    String testString = "testing";
    WebSpiderExample.isNum(testString);
    fail("fail to test for isNumber");
    String testNumber = "";
    WebSpiderExample.isNum(testNumber);
  }
  
  /**
   * A demonstration to test the find total page that points to one link.
   * @throws Exception if problems occur
   */
  @Test
  public void testLogList() throws Exception {
    String[] test = new String[4];
    test[0] = "-mostpopular";
    test[1] = "http://hackystat.org/hackyDevSite/home.do";
    test[2] = "5";
    test[3] = "-logging";
    
    WebSpiderExample.main(test);
    assertEquals ("test urls", testUrl, testWebSpider.startUrl);
    
    test[3] = "logging";
    WebSpiderExample.main(test); 
  }
  
  /**
   * A demonstration to test the start url.
   */
  @Test
  public void testStartUrl() {
    WebSpiderExample testWebSpider = new WebSpiderExample("http://www.httpunit.org");
    assertNotNull("Testing the start url", testWebSpider.startUrl);
  }
  
  /**
   * A demonstration to test the traverse pages.
   * @throws Exception if problems occur retrieving the number of links
   */
  @Test
  public void testTraversePages() throws Exception {
    WebSpiderExample wse = new WebSpiderExample("http://hackystat.org");
    wse.traversePages("http://hackystat.org/hackyDevSite/home.do", 3);
    assertEquals ("test", testUrl, testWebSpider.startUrl);
  }  
}














